﻿using System;
using System.Collections.Generic;
using System.Data;
namespace AutoTool
{
    public static class DataMapper
    {
        public static List<T> MapToList<T>(DataTable table) where T : new()
        {
            var list = new List<T>();

            foreach (DataRow row in table.Rows)
            {
                T obj = new T();
                foreach (var property in typeof(T).GetProperties())
                {
                    if (table.Columns.Contains(property.Name))
                    {
                        if (row[property.Name] == DBNull.Value)
                        {
                            // 处理 DBNull 的情况
                            if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                            {
                                property.SetValue(obj, null);
                            }
                        }
                        else
                        {
                            // 处理可空类型的转换
                            if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                            {
                                var underlyingType = Nullable.GetUnderlyingType(property.PropertyType);
                                object value = Convert.ChangeType(row[property.Name], underlyingType);
                                property.SetValue(obj, value);
                            }
                            else
                            {
                                property.SetValue(obj, Convert.ChangeType(row[property.Name], property.PropertyType));
                            }
                        }
                    }
                }
                list.Add(obj);
            }

            return list;
        }

        public static T MapToObject<T>(DataRow row) where T : new()
        {
            T obj = new T();
            foreach (var property in typeof(T).GetProperties())
            {
                if (row.Table.Columns.Contains(property.Name))
                {
                    if (row[property.Name] == DBNull.Value)
                    {
                        // 处理 DBNull 的情况
                        if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                        {
                            property.SetValue(obj, null);
                        }
                    }
                    else
                    {
                        // 处理可空类型的转换
                        if (property.PropertyType.IsGenericType && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>))
                        {
                            var underlyingType = Nullable.GetUnderlyingType(property.PropertyType);
                            object value = Convert.ChangeType(row[property.Name], underlyingType);
                            property.SetValue(obj, value);
                        }
                        else
                        {
                            property.SetValue(obj, Convert.ChangeType(row[property.Name], property.PropertyType));
                        }
                    }
                }
            }
            return obj;
        }
    }

}
